# -*- coding: utf-8 -*-

from fastapi_limiter.depends import RateLimiter
from fastapi import APIRouter, Depends

from App.common.respones.response_schema import response_base, ResponseSchemaModel, ResponseModel
from App.common.security.jwt import DependsJwtAuth

# 动态导入模型和Schema
from App.app.admin.services.sys_config_service import SysConfigService
from App.app.admin.schemas.sys_config_schema import (
    UpdateSysConfigParam, SysConfigSchema,
)
from App.config.log_conf import log

router = APIRouter(prefix="/config")


@router.get(
    "",
    summary='获取平台配置详情',
    description='获取平台配置详情',
    dependencies=[Depends(RateLimiter(times=20, minutes=1))]
)
async def get_sys_config() -> ResponseSchemaModel[SysConfigSchema]:
    sys_config = await SysConfigService.get()
    return response_base.success(data=sys_config)


@router.put(
    '',
    summary='修改平台配置',
    description='修改平台配置',
    dependencies=[Depends(RateLimiter(times=20, minutes=1)), DependsJwtAuth]
)
async def update_sys_config(obj_in: UpdateSysConfigParam) -> ResponseModel:
    try:
        await SysConfigService.update(obj_in=obj_in)
        return response_base.success()
    except Exception as e:
        log.error(e)
        return response_base.fail()
